package com.itheima.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


/**
 * @author wuzhouwei
 * @date 2020/4/15
 */
public class ClearOrderSettingJob {

    @Reference
    private OrderSettingService orderSettingService;

    @Autowired
    private JedisPool jedisPool;

    public synchronized void clearOrderSetting() {
        try {
            LocalDate today = LocalDate.now();
            // 上个月的年-月-号   2020-03-15
            today = today.minusMonths(1);
            // 这里把上个月日期格式化成2020-03-
            String upYearMonth = DateTimeFormatter.ofPattern("yyyy-MM-").format(today);
            int count = orderSettingService.findCountUpYearMonth(upYearMonth);
            String isClearOrderSetting = jedisPool.getResource().get("isClearOrderSetting");
            if ("1".equals(isClearOrderSetting)) {
                if (count > 0) {
                    orderSettingService.copyOrderSettingToDuplicate(upYearMonth);
                    orderSettingService.deleteByUpYearMonth(upYearMonth);
                    System.out.println("【预约设置定时清理】 " + "清理一次" + upYearMonth + "预约设置");
                } else {
                    System.out.println("【预约设置定时清理】 " + upYearMonth + "没有预约设置数据");
                }
            }else {
                System.out.println("【预约设置定时清理】 " + "亲!  定时已开启,但不允许清理预约设置!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
